package com.vincent.kdp.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.commodity.CommodityDetailActivity;
import com.vincent.kdp.ui.other.MainActivity;
import com.vincent.kdp.util.BaseParam;
import com.vincent.kdp.util.DataCleanManager;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends Activity {


    @Bind(R.id.version)
    TextView version;
    @Bind(R.id.cache_size)
    TextView cacheSize;
    @Bind(R.id.back_img)
    ImageView back;
    @Bind(R.id.toolbar_center_tv)
    TextView toolbarCenterTextView;
    @Bind(R.id.clearCache)
    RelativeLayout clearCache;
    @Bind(R.id.config_uhf)
    RelativeLayout configUhf;
    @Bind(R.id.my_setting)
    LinearLayout checkLevel;
    @Bind(R.id.switch_update)
    Switch switch_update;
    private PackageInfo packageInfo;
    private String myVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting2);
        ButterKnife.bind(this);
        toolbarCenterTextView.setText("系统设置");
        initData();
        if(SPUtil.getInt("autoUpdate")==1){
            switch_update.setChecked(true);
        }else if(SPUtil.getInt("autoUpdate")==2){
            switch_update.setChecked(false);
        }
        switch_update.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SPUtil.put("autoUpdate", 1);
                    Toast.makeText(SettingActivity.this,"已开启更新提示",Toast.LENGTH_SHORT).show();
                } else {
                    SPUtil.put("autoUpdate", 2);
                    Toast.makeText(SettingActivity.this,"已关闭更新提示",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initData() {
        myVersion = getVersion();
        version.setText("V" + myVersion);
        //获取缓存大小
        try {
            String cc = DataCleanManager.getTotalCacheSize(this);
            cacheSize.setText(cc);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void configUhf() {
        Intent intent = new Intent(SettingActivity.this, ConnectUhfActiviy.class);
        startActivity(intent);
    }

    @OnClick({R.id.clearCache, R.id.back_img, R.id.config_uhf})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.clearCache:
                DataCleanManager.cleanDatabases(this);
                cacheSize.setText("0KB");
                break;
            case R.id.back_img:
                finish();
                break;
            case R.id.config_uhf:
                configUhf();
                break;
        }
    }
    /**
     * 关开启自动更新
     */
    /**
     * 获取本地版本号
     */
    public String getVersion() {
        PackageManager pack = getPackageManager();
        try {
            packageInfo = pack.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        Log.i("https", packageInfo.versionName);

        return packageInfo.versionName;
    }
}
